<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tenant:app:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appList">
      <el-table-column label="主键" align="center" prop="id"/>
      <el-table-column label="名称" align="center" prop="name" :show-overflow-tooltip="true"/>
      <el-table-column label="应用编码" align="center" prop="app_key" :show-overflow-tooltip="true"/>
      <el-table-column label="白名单" align="center" prop="white_ip" :show-overflow-tooltip="true"/>
      <el-table-column label="logo" align="center" prop="logo" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-popover trigger="click">
            <a :href="scope.row.logo" target="_blank" title="查看大图">
              <img :src="scope.row.logo" alt=""
                   style="width: 350px; height: 350px; cursor: pointer"/>
            </a>
            <img slot="reference" :src="scope.row.logo" alt=""
                 style="width: 40px; height: 40px; cursor: pointer"/>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="安全时间" align="center" prop="safe_day" :show-overflow-tooltip="true"/>
      <el-table-column label="累计收入" align="center" prop="total_income" :show-overflow-tooltip="true"/>
      <el-table-column label="客服链接" align="center" prop="customer_service_url" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="显示顺序" prop="order_num" width="100"/>
      <el-table-column label="创建时间" align="center" prop="create_time" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tenant:app:edit']"
          >修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.page_size"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="租户" prop="group_id" v-if="form.id == undefined">
          <el-select
            v-model="form.group_id"
            filterable
          >
            <el-option
              v-for="item in tenant_list"
              :key="item.group_id"
              :label="item.name"
              :value="item.group_id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" maxlength="30"/>
        </el-form-item>
        <el-form-item label="应用类型" prop="app_flag" v-if="form.id == undefined">
          <el-radio-group v-model="form.app_flag">
            <el-radio
              v-for="dict in dict.type.app_flag"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="logo" prop="logo" v-if="form.id !== undefined">
          <el-upload
            v-model="form.logo"
            drag
            :data="qiniuData"
            :action="qiniuData.upload_url"
            :before-remove="beforeRemove"
            :on-remove="handleRemove"
            :before-upload="beforeUpload"
            :on-success="uploadSuccess"
            :on-error="uploadError"
            :limit="1"
            :file-list="form.logo_file_list"
            list-type="picture"
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">文件不得超过5M</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="安全时间" prop="safe_day" v-if="form.id !== undefined">
          <el-input type="number" v-model="form.safe_day" placeholder="请输入安全时间"/>
        </el-form-item>
        <el-form-item label="累计收入" prop="total_income" v-if="form.id !== undefined">
          <el-input type="number" v-model="form.total_income" placeholder="请输入累计收入"/>
        </el-form-item>
        <el-form-item label="邀请域名" prop="referral_domain" v-if="form.id !== undefined">
          <el-input type="textarea" v-model="form.referral_domain" placeholder="请输入邀请域名"/>
        </el-form-item>
        <el-form-item label="客服链接" prop="customer_service_url" v-if="form.id !== undefined">
          <el-input type="textarea" v-model="form.customer_service_url" placeholder="请输入客服链接(,隔开)"/>
        </el-form-item>
        <el-form-item label="投资账户开关" prop="account_invest" v-if="form.id !== undefined">
          <el-radio-group v-model="form.account_invest">
            <el-radio
              v-for="dict in dict.type.app_account_invest"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="最低充值金额" prop="limit_min_deposit_amount" v-if="form.id !== undefined">
          <el-input type="number" step="0.01" v-model="form.limit_min_deposit_amount" placeholder="请输入最低充值金额"/>
        </el-form-item>
        <el-form-item label="提现配置" v-if="form.id !== undefined">
          <el-form-item label="首次提现最低金额" prop="first_withdraw_amount">
            <el-input type="number" step="0.01" v-model="form.first_withdraw_amount" placeholder="请输入首次提现金额"/>
          </el-form-item>
          <el-form-item label="首次提现最高金额" prop="first_withdraw_max_amount">
            <el-input type="number" step="0.01" v-model="form.first_withdraw_max_amount"
                      placeholder="请输入首次提现最高金额"/>
          </el-form-item>
          <el-form-item label="最低提现金额" prop="limit_min_withdraw_amount">
            <el-input type="number" step="0.01" v-model="form.limit_min_withdraw_amount"
                      placeholder="请输入最低提现金额"/>
          </el-form-item>
          <el-form-item label="提现限制" prop="withdraw_fee_amount">
            <el-input type="number" step="0.01" v-model="form.withdraw_fee_amount"
                      placeholder="请输入提现限制"/>
          </el-form-item>
          <el-form-item label="提现手续费" prop="withdraw_fee">
            <el-input type="number" step="0.01" v-model="form.withdraw_fee" placeholder="请输入提现手续费"/>
          </el-form-item>
          <el-form-item label="提现手续费(%)" prop="withdraw_fee_rate">
            <el-input type="number" step="0.01" v-model="form.withdraw_fee_rate" placeholder="请输入提现手续费(%)"/>
          </el-form-item>
          <el-form-item label="提现周期">
            <el-select v-model="form.withdraw_week" placeholder="提现周期" clearable multiple>
              <el-option
                v-for="dict in dict.type.withdraw_week"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="提现开始时间" prop="withdraw_start">
            <el-time-picker
              v-model="form.withdraw_start"
              placeholder="提现开始时间"
              value-format="HH:mm:ss"
            >
            </el-time-picker>
          </el-form-item>
          <el-form-item label="提现结束时间" prop="withdraw_end">
            <el-time-picker
              v-model="form.withdraw_end"
              placeholder="提现结束时间"
              value-format="HH:mm:ss"
            >
            </el-time-picker>
          </el-form-item>
        </el-form-item>
        <el-form-item label="提现开关" prop="withdraw" v-if="form.id !== undefined">
          <el-radio-group v-model="form.withdraw">
            <el-radio
              v-for="dict in dict.type.app_withdraw"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="提现最高金额" prop="withdraw_min_amount" v-if="form.withdraw == 1">
          <el-input type="number" step="0.01" v-model="form.withdraw_min_amount" placeholder="请输入提现最高金额"/>
        </el-form-item>
        <el-form-item label="归集开关" prop="gather" v-if="form.id !== undefined">
          <el-radio-group v-model="form.gather">
            <el-radio
              v-for="dict in dict.type.app_gather"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="归集最低金额" prop="gather_min_amount" v-if="form.gather == 1">
          <el-input type="number" step="0.01" v-model="form.gather_min_amount" placeholder="请输入归集最低金额"/>
        </el-form-item>
        <el-form-item label="归集用户名" prop="trx_energy_username" v-if="form.gather == 1">
          <el-input v-model="form.trx_energy_username" placeholder="请输入用户名"/>
        </el-form-item>
        <el-form-item label="归集密码" prop="trx_energy_password" v-if="form.gather == 1">
          <el-input v-model="form.trx_energy_password" placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item label="归集资源类型" prop="trx_energy_resType" v-if="form.gather == 1">
          <el-input v-model="form.trx_energy_resType" placeholder="请输入资源类型"/>
        </el-form-item>
        <el-form-item label="归集租用数量" prop="trx_energy_payNums" v-if="form.gather == 1">
          <el-input v-model="form.trx_energy_payNums" placeholder="请输入租用数量"/>
        </el-form-item>
        <el-form-item label="归集租用小时" prop="trx_energy_rentTime" v-if="form.gather == 1">
          <el-input v-model="form.trx_energy_rentTime" placeholder="请输入租用小时"/>
        </el-form-item>
        <el-form-item label="归集租用锁定" prop="trx_energy_resLock" v-if="form.gather == 1">
          <el-input v-model="form.trx_energy_resLock" placeholder="请输入租用锁定"/>
        </el-form-item>
        <el-form-item label="验证码" prop="google_code" v-if="form.id !== undefined">
          <el-input v-model="form.google_code" placeholder="请输入验证码" maxlength="30"/>
        </el-form-item>
        <el-form-item label="抽奖开关" prop="lucky_draw_open">
          <el-radio-group v-model="form.lucky_draw_open">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <span style="color:red">开启默认一天提现一次</span>
        <el-form-item label="提现间隔开关" prop="withdraw_interval">
          <el-radio-group v-model="form.withdraw_interval">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="用户提现限制id集合" prop="limit_user_ids">
          <el-input v-model="form.limit_withdraw_user_ids" placeholder="请输入用户提现限制id集合,英文逗号隔开"></el-input>
        </el-form-item>
        <el-form-item label="用户量化限制id集合" prop="limit_user_ids">
          <el-input v-model="form.limit_quantification_user_ids" placeholder="请输入用户量化限制id集合,英文逗号隔开"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="submit_loading">{{ submit_text }}</el-button>
        <el-button ref="cancelButton" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addApp, getApp, listApp, updateApp} from "@/api/metadata/app";
import {getTenantList} from "@/api/tenant/tenant";
import {getLightPictureToken} from "@/api/file/file";

export default {
  name: "App",
  computed: {},
  dicts: ['sys_normal_disable', 'app_gather', 'app_flag', 'app_withdraw', 'withdraw_week', 'app_account_invest'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      appList: [],
      submit_loading: false,
      submit_text: '确 定',
      tenant_list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      qiniuData: {
        key: undefined,
        url: undefined,
        upload_url: undefined,
      },
      // 查询参数
      queryParams: {
        page: 1,
        page_size: 10,
        name: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        group_id: [
          {required: true, message: "租户不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ],
        app_flag: [
          {required: true, message: "应用类型不能为空", trigger: "blur"}
        ],
        customer_service_url: [
          {required: true, message: "客服链接不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "状态不能为空", trigger: "blur"}
        ],
        white_ip: [
          {required: true, message: "域名不能为空", trigger: "blur"}
        ],
        logo: [
          {required: true, message: "logo不能为空", trigger: "blur"}
        ],
        safe_day: [
          {required: true, message: "安全时间不能为空", trigger: "blur"}
        ],
        total_income: [
          {required: true, message: "累计收入不能为空", trigger: "blur"}
        ],
        withdraw: [
          {required: true, message: "提现开关不能为空", trigger: "blur"}
        ],
        withdraw_min_amount: [
          {required: true, message: "提现最高金额不能为空", trigger: "blur"}
        ],
        account_invest: [
          {required: true, message: "投资账户开关不能为空", trigger: "blur"}
        ],
        gather: [
          {required: true, message: "归集开关不能为空", trigger: "blur"}
        ],
        gather_min_amount: [
          {required: true, message: "归集最低金额不能为空", trigger: "blur"}
        ],
        trx_energy_username: [
          {required: true, message: "归集用户名不能为空", trigger: "blur"}
        ],
        trx_energy_password: [
          {required: true, message: "归集密码不能为空", trigger: "blur"}
        ],
        trx_energy_resType: [
          {required: true, message: "归集资源类型不能为空", trigger: "blur"}
        ],
        trx_energy_payNums: [
          {required: true, message: "归集租用数量不能为空", trigger: "blur"}
        ],
        trx_energy_rentTime: [
          {required: true, message: "归集租用小时不能为空", trigger: "blur"}
        ],
        trx_energy_resLock: [
          {required: true, message: "归集租用锁定不能为空", trigger: "blur"}
        ],
        limit_min_deposit_amount: [
          {required: true, message: "最低充值金额不能为空", trigger: "blur"}
        ],
        first_withdraw_amount: [
          {required: true, message: "首次提现金额不能为空", trigger: "blur"}
        ],
        first_withdraw_max_amount: [
          {required: true, message: "首次提现最高金额不能为空", trigger: "blur"}
        ],
        limit_min_withdraw_amount: [
          {required: true, message: "最低提现金额不能为空", trigger: "blur"}
        ],
        withdraw_fee_amount: [
          {required: true, message: "提现限制不能为空", trigger: "blur"}
        ],
        withdraw_fee: [
          {required: true, message: "提现手续费不能为空", trigger: "blur"}
        ],
        withdraw_fee_rate: [
          {required: true, message: "提现手续费(%)不能为空", trigger: "blur"}
        ],
        google_code: [
          {required: true, message: 'google验证码不能为空', trigger: 'blur'},
        ],
        withdraw_week: [
          {required: true, message: '提现周期不能为空', trigger: 'blur'},
        ],
        withdraw_start: [
          {required: true, message: '提现开始不能为空', trigger: 'blur'},
        ],
        withdraw_end: [
          {required: true, message: '提现结束不能为空', trigger: 'blur'},
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getTenantList();
    this.getFileToken();
  },
  methods: {
    changeMessage() {
      this.$forceUpdate()
    },
    // 上传前
    beforeUpload(file) {
      const isPNG = file.type === 'image/png';
      const isJPEG = file.type === 'image/jpeg';
      const isJPG = file.type === 'image/jpg';
      const isLt2M = file.size / 1024 / 1024 < 5;
      if (!isPNG && !isJPEG && !isJPG) {
        this.$modal.msgError("上传图片只能是 jpg、png、jpeg 格式!");
        return false;
      }
      if (!isLt2M) {
        this.$modal.msgError("上传图片大小不能超过 5MB!");
        return false;
      }
    },
    uploadError(err, file, fileList) {
      this.$modal.msgError("上传出错，请重试！");
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    uploadSuccess(response, file, fileList) {
      this.form.logo = `${response.data.url}`;
    },
    // 删除图片
    handleRemove(file, fileList) {
      this.form.logo = undefined
      this.$modal.msgSuccess('删除成功')
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listApp(this.queryParams).then(response => {
          this.appList = response.data;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    getTenantList() {
      getTenantList().then(response => {
          this.tenant_list = response.data;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        group_id: undefined,
        app_flag: 'global_finance',
        name: undefined,
        white_ip: undefined,
        logo: undefined,
        logo_file_list: [],
        customer_service_url: undefined,
        referral_domain: undefined,
        order_num: 0,
        status: 1,
        lucky_draw_open: undefined,
        withdraw: undefined,
        withdraw_min_amount: undefined,
        account_invest: undefined,
        gather: undefined,
        gather_min_amount: undefined,
        trx_energy_username: undefined,
        trx_energy_password: undefined,
        trx_energy_resType: undefined,
        trx_energy_payNums: undefined,
        trx_energy_rentTime: undefined,
        trx_energy_resLock: undefined,
        limit_min_deposit_amount: undefined,
        first_withdraw_amount: undefined,
        first_withdraw_max_amount: undefined,
        limit_min_withdraw_amount: undefined,
        withdraw_fee_amount: undefined,
        withdraw_fee: undefined,
        withdraw_fee_rate: undefined,
        withdraw_week: undefined,
        withdraw_start: undefined,
        withdraw_end: undefined,
        google_code: '',
        withdraw_interval: undefined,
        limit_withdraw_user_ids: undefined,
        limit_quantification_user_ids: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createTimeRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApp(id).then(response => {
        this.form = response.data;
        if (response.data.trx_energy.username !== undefined) {
          this.form.trx_energy_username = response.data.trx_energy.username;
        }
        if (response.data.trx_energy.password !== undefined) {
          this.form.trx_energy_password = response.data.trx_energy.password;
        }
        if (response.data.trx_energy.resType !== undefined) {
          this.form.trx_energy_resType = response.data.trx_energy.resType;
        }
        if (response.data.trx_energy.payNums !== undefined) {
          this.form.trx_energy_payNums = response.data.trx_energy.payNums;
        }
        if (response.data.trx_energy.rentTime !== undefined) {
          this.form.trx_energy_rentTime = response.data.trx_energy.rentTime;
        }
        if (response.data.trx_energy.resLock !== undefined) {
          this.form.trx_energy_resLock = response.data.trx_energy.resLock;
        }
        if (this.form.logo !== undefined) {
          var imageArr = this.form.logo.split('/')
          this.form.logo_file_list = [
            {
              'url': this.form.logo,
              'name': imageArr[imageArr.length - 1]
            }
          ]
        }
        this.open = true;
        this.title = "修改参数";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateApp(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.submit_loading = false;
              this.getList();
            });
          } else {
            addApp(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.submit_loading = false;
              this.getList();
            });
          }
        }
      });
    },
    getFileToken() {
      getLightPictureToken().then((response) => {
        this.qiniuData.key = response.data.token;
        this.qiniuData.url = response.data.url;
        this.qiniuData.upload_url = response.data.upload_url;
      })
    }
  }
};
</script>
